<?php
$page_type = "A";
require_once 'header.php';

if (!empty($_GET["update_error"]))
  $msg = "<font color=\"#CC0000\">{$_GET["update_error"]}</font>";
else
  $msg = '';
  
// Verify where we are from, student.php or student_update_response.php
if (!isset($_GET["update_error"])) { // From student.php
  // Fetch the record to be updated
  $input_sid = $_GET["sid"];
  $input_seqid = $_GET["seqid"];

  $sql = "select Fname, Lname, C.Sid, STFlag, PFlag, Cno, Grade, T.Seqid from CLIENT C, TAKEN T, COURSE_OFFERING CO where C.Sid=T.Sid and T.Seqid=CO.Seqid and C.Sid='$input_sid' and T.Seqid='$input_seqid'";

  $result_array = execute_sql_in_oracle($sql);
  $result = $result_array["flag"];
  $cursor = $result_array["cursor"];

  if (!$result){
    display_oracle_error_message($cursor);
    die("Query using inputted student ID and course sequence ID failed.");
  }

  // Get student information from database query result
  $values = oci_fetch_array($cursor);
  oci_free_statement($cursor);

  $fname = $values[0];
  $lname = $values[1];
  $sid = $values[2];
  $stflag = $values[3];
  $pflag = $values[4];
  $cno = $values[5];
  $grade = $values[6];
  $seqid = $values[7];
	
  // Student type
  if ($stflag == "0")
    $student_type = "U";
  else if ($stflag == "1")
    $student_type = "G";
  else
    $student_type = "";
	
  // Probation status
  if ($pflag == "0")
    $probation = "n";
  else if ($pflag == "1")
    $probation = "y";
  else
    $probation = "";
}
else { // From student_update_response.php
  // Initialize field variables
  $fname = "";
  $lname = "";
  $sid = "";
  $student_type = "";
  $probation = "";
  $cno = "";
  $grade = "";
  $seqid = "";
  
  // Obtain values of the record to be updated directly
  if (isset($_SESSION['fname']))
    $fname = $_SESSION['fname'];
  if (isset($_SESSION['lname']))
    $lname = $_SESSION['lname'];
  if (isset($_SESSION['sid']))
    $sid = $_SESSION['sid'];
  if (isset($_SESSION['student_type']))
    $student_type = $_SESSION['student_type'];
  if (isset($_SESSION['input_pflag']))
    $probation = $_SESSION['input_pflag'];
  if (isset($_SESSION['input_grade']))
    $grade = $_SESSION['input_grade'];
  if (isset($_SESSION['input_cno']))
    $cno = $_SESSION['input_cno'];
  if (isset($_SESSION['input_seqid']))
    $seqid = $_SESSION['input_seqid'];
}

echo "<h2>Student Record Update Form</h2>";

// Display the record to be updated
echo("
  <form method=\"post\" action=\"student_update_response.php\">
  SID (Read-only): <input type=\"text\" readonly value =\"$sid\" size=\"20\" maxlength=\"10\" name=\"sid\"><br> 
  First Name (Required): <input type=\"text\" value = \"$fname\" size=\"20\" maxlength=\"30\" name=\"fname\">  <br>
  Last Name (Required): <input type=\"text\" value = \"$lname\" size=\"20\" maxlength=\"30\" name=\"lname\">  <br>
  Course Sequence ID (Read-only): <input type=\"text\" readonly value =\"$seqid\" size=\"20\" maxlength=\"10\" name=\"seqid\"><br>
  Course number (Read-only): <input type=\"text\" readonly value =\"$cno\" size=\"20\" maxlength=\"10\" name=\"cno\"><br>
  Grade (Read-only): <input type=\"text\" readonly value =\"$grade\" size=\"20\" maxlength=\"3\" name=\"grade\"><br>");
  
// Reselect student type dropdownlist after postback
if ($student_type=="") 
  $choose_one_select1 = htmlspecialchars("selected");
else if ($student_type=="U") 
  $u_selection = htmlspecialchars("selected");
else if ($student_type=="G") 
  $g_selection = htmlspecialchars("selected");
  
// Create the dropdown list for the student type
echo("
  Student Type:
  <select name=\"student_type\">
  <option value=\"\" $choose_one_select1>Choose One</option>
  <option value=\"U\" $u_selection>Undergraduate</option>
  <option value=\"G\" $g_selection>Graduate</option></select>");
  
// Reselect probation status dropdownlist after postback
if ($probation=="") 
  $choose_one_select2 = htmlspecialchars("selected");
else if ($probation=="y") 
  $yp_selection = htmlspecialchars("selected");
else if ($probation=="n") 
  $np_selection = htmlspecialchars("selected");
  
// Create the dropdown list for the probation status
echo("
  Probation Status:
  <select name=\"probation\">
  <option value=\"\" $choose_one_select2>Choose One</option>
  <option value=\"y\" $yp_selection>Yes</option>
  <option value=\"n\" $np_selection>No</option>
  ");

echo("
  </select>  <input type=\"submit\" value=\"Update\">
  <input type=\"reset\" value=\"Reset\">
  </form>

  <form method=\"post\" action=\"student.php\">
  <input type=\"submit\" value=\"Go Back\">
  </form>$msg");
?>